﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Nirvana
{
	[Serializable]
	public class LuaCheckReportItem
	{
		public enum ServerityType
		{
			Warn,
			Error
		}

		[SerializeField]
		private ServerityType severity;

		[SerializeField]
		private string path;

		[SerializeField]
		private int row;
		[SerializeField]
		private int column;
		[SerializeField]
		private string message;

        [SerializeField]
        private string mAssetGUID;


        public ServerityType Severity { get { return severity; } set { this.severity = value; } }
		public string Path { get { return path; } set { this.path = value; } }
		public int Row { get { return row; } set { this.row = value; } }
		public int Column { get { return column; } set { this.column = value; } }
		public string Message { get { return message; } set { this.message = value; } }

        public static Vector2 Show(List<LuaCheckReportItem> itemlist, Vector2 pos, ref int index)
        {
            var itemui = new ReportItemUI();
            itemui.curIndex = index;
            pos = GUILayoutEx.ScrollViewFixHeight<LuaCheckReportItem>(pos, itemlist, 1.75f * EditorGUIUtility.singleLineHeight,600, itemui.Draw);
            index = itemui.curIndex;
            return pos;
        }

        private int Draw(LuaCheckReportItem item,int curIndex, int startIndex)
        {
            if (curIndex == startIndex)
            {
                GUILayout.BeginHorizontal(LuaCheckReportItemGUIStyle.Gray,
                    GUILayout.MinHeight(1.75f * EditorGUIUtility.singleLineHeight),
                    GUILayout.MaxHeight(1.75f * EditorGUIUtility.singleLineHeight)
                );
            }
            else
            {
                bool flag = curIndex % 2 == 0;
                GUILayout.BeginHorizontal(!flag ? LuaCheckReportItemGUIStyle.Gray03 : LuaCheckReportItemGUIStyle.Gray02,
                    GUILayout.MinHeight(1.75f * EditorGUIUtility.singleLineHeight),
                    GUILayout.MaxHeight(1.75f * EditorGUIUtility.singleLineHeight)
                );

            }

            if (item.severity == ServerityType.Error)
            {
                GUILayout.Label(LuaCheckReportItemGUIStyle.errorSml, GUILayout.ExpandWidth(false));
            }
            else
            {
                GUILayout.Label(LuaCheckReportItemGUIStyle.warnSml, GUILayout.ExpandWidth(false));
            }

            string msg = string.Format("{0}({1},{2}):\n{3}", item.path, item.row, item.column, item.message);

            GUILayout.Label(msg, LuaCheckReportItemGUIStyle.richText, GUILayout.ExpandHeight(true));
            Rect lastRect = GUILayoutUtility.GetLastRect();
            if (Event.current.button == 0 && lastRect.Contains(Event.current.mousePosition))
            {
                if (Event.current.type == EventType.MouseUp)
                {
                    startIndex = curIndex;
                    var obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(item.path);
                    if (obj != null)
                    {
                        EditorGUIUtility.PingObject(obj);
                    }
                    Event.current.Use();
                }
                else if (Event.current.type == EventType.MouseDown && Event.current.clickCount == 2)
                {
                    //TextEditorTool.OpenText(item.path,item.row,item.column); //双击则启动txt编辑器打开lua
                    Event.current.Use();
                }
            }

            GUILayout.EndHorizontal();
            return startIndex;
        }

        private static class LuaCheckReportItemGUIStyle
        {
            public static GUIStyle Gray, Gray03, Gray02, richText;
            public static GUIContent warnSml, errorSml;
            static LuaCheckReportItemGUIStyle()
            {
                Gray = new GUIStyle { border = new RectOffset(0, 0, 0, 0) };
                Gray.normal.background = TextureMaker.Monochromatic(new Color(0.1f, 0.1f, 0.35f));
                Gray03 = new GUIStyle { border = new RectOffset(0, 0, 0, 0) };
                Gray03.normal.background = TextureMaker.Gray(0.3f);
                Gray02 = new GUIStyle { border = new RectOffset(0, 0, 0, 0) };
                Gray02.normal.background = TextureMaker.Gray(0.2f);

                richText = new GUIStyle { richText = true };
                richText.normal.textColor = new Color(0.75f, 0.75f, 0.75f, 1f);

                warnSml = EditorGUIUtility.IconContent("console.warnicon.sml");
                errorSml = EditorGUIUtility.IconContent("console.erroricon.sml");
            }
        }

        private sealed class ReportItemUI
        {
            public int curIndex;
            public void Draw(LuaCheckReportItem item, int startIndex)
            {
                this.curIndex = item.Draw(item,startIndex, this.curIndex);
              
            }
        }
    }
}
